<template>
  <div class="wrapper">
    <el-carousel ref="carousel" :interval="playInterval" :height="screenHeight" arrow="never" 
    @mouseenter.native="delHandleMouseEnter" @change="handleCarouselChange" 
    :autoplay="autoPlayFlag" :loop="false" indicator-position="none">
      <el-carousel-item v-for="(item, index) in bannerList" :key="index">
        <img style="width: 100%; height: 100%;" :src="item.image" alt="">
      </el-carousel-item>
    </el-carousel>
  </div>
</template>

<script>

export default {
  name: "Banner",
  props: {
    bannerList: {
      default: [],
    },
  },
  components: {

  },
  data() {
    return {
      screenHeight: '',
      autoPlayFlag: false,
      playInterval: 15000,
    }
  },
  computed: {

  },
  watch: {},
  methods: {
    delHandleMouseEnter() {
      this.$refs.carousel.handleMouseEnter = () => { };
    },
    startPlay() {
      if (this.bannerList.length == 4) {
        this.playInterval = 10000
      }
      this.startItem(0)
      this.autoPlayFlag = true

      if (this.bannerList.length == 1) {
        let that = this
        setTimeout(() => {
          that.$emit('imgEnd')
        }, that.playInterval);
      }
    },
    startItem(index) {
      this.$refs.carousel.setActiveItem(index)
    },
    handleCarouselChange(val) {
      console.log('切换')
      if (val == this.bannerList.length - 1) {
        let that = this
        setTimeout(() => {
          that.$emit('imgEnd')
        }, that.playInterval);
      }
    }
  },
  created() {
    this.screenHeight = window.screen.height + 'px'
    console.log(this.screenHeight)
  },
  mounted() {
    this.$refs.carousel.handleMouseEnter = () => { };
  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>

<style lang='scss' scoped>
  .wrapper {
    width: 100%;
    height: 100%;
  }
</style>